<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="房屋编号" prop="houseId">
        <el-input
          v-model="queryParams.houseId"
          placeholder="请输入房屋编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList">
      <el-table-column label="合同编号" align="center" prop="contractId"/>
      <el-table-column label="合同状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.house_contract_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-claim"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['domain:contract:edit']"
            v-if="scope.row.status == 2"
          >生成合同
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-user"
            @click="handleUserDetail(scope.row)"
            v-hasPermi="['domain:contract:query']"
            v-if="scope.row.status == 2"
          >用户详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="ContractDetail(scope.row)"
            v-hasPermi="['domain:contract:query']"
            v-if="scope.row.status != 2"
          >合同详情
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleReport(scope.row)"
            v-hasPermi="['domain:report:add']"
            v-if="scope.row.status == 0"
          >我要报障
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-error"
            @click="handleQuitApp(scope.row)"
            v-hasPermi="['domain:quit:add']"
            v-if="scope.row.status == 0"
          >申请退租
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-chat-dot-round"
            @click="handleAddComment(scope.row)"
            v-hasPermi="['domain:comment:add']"
          >评论房屋
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['domain:contract:remove']"
            v-if="scope.row.status == 1"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 生成合同信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.houseId" disabled/>
        </el-form-item>
        <el-form-item label="乙方用户编号(租客)" prop="bUserId">
          <el-input v-model="form.bUserId" disabled/>
        </el-form-item>
        <el-form-item label="遵守条例" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 合同详情对话框 -->
    <el-dialog :title="title" :visible.sync="contractDetail" width="650px" append-to-body>
      <el-form ref="form" :model="form" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="form.houseId" disabled/>
        </el-form-item>
        <el-form-item label="甲方用户编号(中介)" prop="aUserId">
          <el-input v-model="form.aUserId" disabled/>
        </el-form-item>
        <el-form-item label="乙方用户编号(租客)" prop="bUserId">
          <el-input v-model="form.bUserId" disabled/>
        </el-form-item>
        <el-form-item label="遵守条例" prop="remark">
          <el-input v-model="form.remark" type="textarea" disabled/>
        </el-form-item>
        <el-form-item label="生效时间" prop="createTime">
          <el-input v-model="form.createTime" disabled/>
        </el-form-item>
        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status" disabled>
            <el-radio
              v-for="dict in dict.type.house_contract_status"
              :key="dict.value"
              :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看用户信息对话框 -->
    <el-dialog :title="title" :visible.sync="userDetail" width="650px" append-to-body>
      <el-form ref="form" :model="user" label-width="150px">
        <el-form-item label="用户昵称" prop="nickName">
          <el-input v-model="user.nickName" disabled/>
        </el-form-item>
        <el-form-item label="用户电话" prop="phone">
          <el-input v-model="user.phone" disabled/>
        </el-form-item>
        <el-form-item label="用户个人证件" prop="documents">
          <el-image :src="options.img" style="width: 350px; height: 250px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--添加申请退租信息-->
    <el-dialog :visible.sync="addQuitApp" :title="title">
      <el-form ref="form" :model="formData" :rules="rules" size="medium" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="formData.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名称" prop="houseName">
          <el-input v-model="formData.houseName" disabled/>
        </el-form-item>
        <el-form-item label="退租时间选择" prop="quitTime">
          <el-date-picker v-model="formData.quitTime"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          :style="{width: '100%'}"
                          :picker-options="pickerOptions"
                          placeholder="请选择退租时间" clearable></el-date-picker>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="formData.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="handelConfirm">提交</el-button>
      </div>
    </el-dialog>

    <!--添加报障信息-->
    <el-dialog :visible.sync="addReport" :title="title">
      <el-form ref="form" :model="reportData" :rules="rules" size="medium" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="reportData.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名称" prop="houseName">
          <el-input v-model="reportData.houseName" disabled/>
        </el-form-item>
        <el-form-item label="报障部位" prop="reportDetail">
          <el-select v-model="reportData.reportDetail" placeholder="请选择报障部位">
            <el-option
              v-for="dict in dict.type.house_report_detail"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="其他" prop="detail" v-if="reportData.reportDetail == 6">
          <el-input v-model="reportData.remark"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirmReport">提交</el-button>
      </div>
    </el-dialog>

    <!--添加房屋评论信息-->
    <el-dialog :visible.sync="addComment" :title="title">
      <el-form ref="form" :model="commentData" :rules="rules" size="medium" label-width="150px">
        <el-form-item label="房屋编号" prop="houseId">
          <el-input v-model="commentData.houseId" disabled/>
        </el-form-item>
        <el-form-item label="房屋名称" prop="houseName">
          <el-input v-model="commentData.houseName" disabled/>
        </el-form-item>
        <el-form-item label="评论信息" prop="commentDetail">
          <el-input v-model="commentData.commentDetail" type="textarea"/>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="confirmComment">发布</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listContract,
  getContract,
  delContract,
  updateContract,
  queryUserDetail,
  getHouseDetail, userAddQuit
} from "@/api/system/contract";
import store from "@/store";
import modal from "@/plugins/modal";
import {addReport} from "@/api/system/report";
import {addComment} from "@/api/system/comment";

export default {
  name: "Contract",
  dicts: ["house_contract_status", "house_is_handle", "house_report_detail"],
  data() {
    return {
      pickerOptions: {
        disabledDate: (time) => {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同信息表格数据
      contractList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      //是否弹出用户信息层
      userDetail: false,
      //是否弹出合同详情层
      contractDetail: false,
      //是否弹出退房申请层
      addQuitApp: false,
      //是否弹出报障层
      addReport: false,
      //是否弹出添加评论层
      addComment: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        houseId: null,
        aUserId: null,
        bUserId: null,
        status: null,
      },
      //退房申请表单参数
      formData: {
        houseId: null,
        houseName: null,
        quitTime: null,
        remark: null
      },
      //报障表单参数
      reportData: {
        houseId: null,
        houseName: null,
        reportDetail: null,
        remark: null
      },
      //评论表单参数
      commentData: {
        houseId: null,
        houseName: null,
        commentDetail: null
      },
      options: {
        img: store.getters.documents, //裁剪图片的地址
      },
      // 表单参数
      form: {},
      //用户信息
      user: {},
      // 表单校验
      rules: {
        remark: [
          {required: true, message: "合同条例不能为空", trigger: "blur"},
          {min: 20, message: '条例字数不得少于20字', trigger: 'blur'}
        ],
        quitTime: [
          {required: true, message: "请选择退租时间!!!", trigger: "blur"},
        ],
        reportDetail: [
          {required: true, message: "请选择报障部位!!!", trigger: "blur"},
        ],
        detail: [
          {required: true, message: "请填写其他保障部位", trigger: "blur"},
        ],
        commentDetail: [
          {required: true, message: "评论信息不能为空", trigger: "blur"},
        ]
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询合同信息列表 */
    getList() {
      this.loading = true;
      const url = window.location.pathname;
      const accessPath = url.substr(url.lastIndexOf("/"), url.length);
      //判断当前页面
      switch (accessPath) {
        //用户
        case "/contract":
          this.queryParams.status = 0;
          break;
        case "/quit_u":
          this.queryParams.status = 1;
          break;
        //中介
        case "/quit_m":
          this.queryParams.status = 1;
          break;
        default:
          break;
      }
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        if (accessPath == '/rentaling') {
          //过滤数组
          for (let i = 0; i < this.contractList.length; i++) {
            if (this.contractList[i].status == 1) {
              this.contractList.splice(i, 1);
            }
          }
        }
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.userDetail = false;
      this.contractDetail = false;
      this.addQuitApp = false;
      this.addReport = false;
      this.addComment = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        houseId: null,
        aUserId: null,
        bUserId: null,
        createTime: null,
        updateTime: null,
        status: "0",
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractId = row.contractId || this.ids
      getContract(contractId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "生成合同信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let form = this.form;
          if (form.contractId != null) {
            this.$modal.confirm("生成合同后无法修改,确定已检查无误吗?").then(function () {
              return updateContract(form);
            }).then(() => {
              this.$modal.msgSuccess("生成合同成功");
              this.open = false;
              this.getList();
            })
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除合同信息编号为"' + contractIds + '"的数据项？').then(function () {
        return delContract(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    //查看用户详情
    handleUserDetail(row) {
      queryUserDetail(row.bUserId).then(response => {
        this.user = response.user;
        this.options.img = process.env.VUE_APP_BASE_API + response.user.documents;
        this.userDetail = true;
        this.title = "查看用户详情信息"
      })
    },
    //查看生效的合同详情
    ContractDetail(row) {
      getContract(row.contractId).then(response => {
        this.form = response.data;
        this.contractDetail = true;
        this.title = "合同详情信息";
      });
    },
    //租客退租申请
    handleQuitApp(row) {
      getHouseDetail(row.houseId).then(response => {
        this.formData = response.house;
        this.formData.remark = null;
        this.addQuitApp = true;
        this.title = "生成退租申请";
      });
    },
    //提交退租申请
    handelConfirm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let formData = this.formData;
          this.$modal.confirm("确定要提交退租申请吗?").then(function () {
            return userAddQuit(formData);
          }).then(() => {
            this.$modal.msgSuccess("提交退租申请成功,请等待中介处理");
            this.addQuitApp = false;
            this.getList();
          })
        }
      });
    },
    //用户报障菜单
    handleReport(row) {
      getHouseDetail(row.houseId).then(response => {
        this.reportData = response.house;
        this.reportData.remark = null;
        this.addReport = true;
        this.title = "生成报障单";
      });
    },
    //租客提交报障信息
    confirmReport() {
      let reportData = this.reportData;
      addReport(reportData).then(response => {
        this.$modal.msgSuccess("提交报障信息成功,请等待中介处理");
        this.addReport = false;
        this.getList();
      })
    },
    //用户打开评论弹窗
    handleAddComment(row) {
      getHouseDetail(row.houseId).then(response => {
        this.commentData = response.house;
        this.commentData.remark = null;
        this.addComment = true;
        this.title = "发布评论信息";
      });
    },
    //用户发布评论
    confirmComment() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let commentData = this.commentData;
          addComment(commentData).then(response => {
            this.$modal.msgSuccess("发布房源信息成功");
            this.addComment = false;
            this.getList();
          })
        }
      });
    }
  }
};
</script>
